<?PHP



/** 
*    Module comments :
*	 Main library for db connectivity and operation
*/ 

// MySQL DB Resource
$db_link = '';



// Disable delayed inserts when not using MyISAM tables
if ($config['table_type'] != 'MYISAM')
	$config['insert_delayed'] = false;



/*********************************************************/
/* Check if the extension is available                   */
/*********************************************************/

function dbAvailable()
{
	return (function_exists('mysql_connect'));
}


/*********************************************************/
/* Open a connection to the database			         */
/*********************************************************/


function dbConnect()
{
	global $config;
	global $db_link;
	
	// Add port to connect, if needed
	if (!isset($config['dbport'])) 
		$config['dbport'] = 3306;

    if ($config['persistent_connections'])
        $db_link = @mysql_pconnect ($config['dbhost'].':'.$config['dbport'], $config['dbuser'], $config['dbpassword']);
    else
        $db_link = @mysql_connect ($config['dbhost'].':'.$config['dbport'], $config['dbuser'], $config['dbpassword']);
	
	if ($config['compatibility_mode'])
		return $db_link;
	
	if (@mysql_select_db ($config['dbname'], $db_link))
		return $db_link;
}



/*********************************************************/
/* Close the connection to the database			         */
/*********************************************************/

function dbClose()
{
	// Never close the database connection, because
	// it may interfere with other scripts which
	// share the same connection.
}



/*********************************************************/
/* Execute a query								         */
/*********************************************************/

function dbQuery($query)
{
    global $last_query;
	global $db_link;
	
	// Connect to the database, if needed
	if (!$db_link &&	!dbConnect())
		return false;
	
    $last_query = $query;
    return @mysql_query ($query, $db_link);
}



/*********************************************************/
/* Get the number of rows returned                       */
/*********************************************************/

function dbNumRows($res)
{
	return @mysql_num_rows($res);
}



/*********************************************************/
/* Get next row as an array with keys                    */
/*********************************************************/

function dbFetchArray($res)
{
	return @mysql_fetch_array($res, MYSQL_ASSOC);
}



/*********************************************************/
/* Get next row as an array                              */
/*********************************************************/

function dbFetchRow($res)
{
	return @mysql_fetch_row($res);
}

/*********************************************************/
/* object, For JSON parsing                             */
/*********************************************************/

function dbFetchObject($res)
{
	return @mysql_fetch_object($res);
}


/*********************************************************/
/* Get a specific row and column                         */
/*********************************************************/

function dbResult($res, $row, $column)
{
	return @mysql_result($res, $row, $column);
}



/*********************************************************/
/* Free the result from memory                           */
/*********************************************************/

function dbFreeResult($res)
{
	return @mysql_free_result($res);
}



/*********************************************************/
/* Return the number of affected rows                    */
/*********************************************************/

function dbAffectedRows()
{
	global $db_link;
	
	return @mysql_affected_rows($db_link);
}



/*********************************************************/
/* Go to the specified row                               */
/*********************************************************/

function dbSeekRow($res, $row)
{
	return @mysql_data_seek($res, $row);
}



/*********************************************************/
/* Get the ID of the last inserted row                   */
/*********************************************************/

function dbInsertID()
{
	global $db_link;
	
	return @mysql_insert_id($db_link);
}



/*********************************************************/
/* Get the error message if something went wrong         */
/*********************************************************/

function dbError ()
{
	global $db_link;
	
	return @mysql_error($db_link);
}

function dbErrorNo ()
{
	global $db_link;
	
	return @mysql_errno($db_link);
}

?>